<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px"
      class="search-form">
      <el-form-item label="记录类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择记录类型" style="width: 100%;" @change="handleTypeChange">
          <el-option label="积分转赠记录" :value="0" />
          <el-option label="会员卡转赠记录" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="转赠人id" prop="transferUserId">
        <el-input v-model="queryParams.transferUserId" placeholder="请输入转赠人id" clearable style="width: 100%;"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="每次消耗积分/SHJC" prop="feeAmount">
        <el-input v-model="queryParams.feeAmount" placeholder="请输入每次消耗积分/SHJC" clearable style="width: 100%;"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="每张消耗积分/SHJC" prop="cardFeeAmount">
        <el-input v-model="queryParams.cardFeeAmount" placeholder="请输入每张消耗积分/SHJC" clearable style="width: 100%;"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单价/SHJC" prop="price">
        <el-input v-model="queryParams.price" placeholder="请输入单价/SHJC" clearable style="width: 100%;"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="转赠时间" prop="createDate">
        <el-date-picker v-model="dateRange" style="width: 100%;" value-format="yyyy-MM-dd HH:mm:ss" type="datetimerange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="数量排序" prop="transferCardCount">
        <el-select v-model="queryParams.cardCountSort" placeholder="请选择排序方式" clearable style="width: 100%;">
          <el-option label="升序" value="asc" />
          <el-option label="降序" value="desc" />
        </el-select>
      </el-form-item>
      <el-form-item style="margin-left: 10px;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:log:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div>
      <span>总次数：{{ totalForm.infoTotal }}</span></br>
      <span>总转赠余额：{{ totalForm.balanceTotal }}</span></br>
      <span>总转赠卡数量：{{ totalForm.cardTotal }}</span>
    </div>

    <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="transferId" /> -->
      <el-table-column label="转赠人id" align="center" prop="transferUserId" />
      <el-table-column label="被转赠人id" align="center" prop="toTransferUserId" />
      <el-table-column label="转赠人姓名" align="center" prop="transferName" />
      <el-table-column label="被转赠人姓名" align="center" prop="toTransferName" />
      <el-table-column v-if="queryParams.type === 1" label="转赠数量/张" align="center" prop="transferCardCount" />
      <el-table-column v-if="queryParams.type === 1" label="单价/SHJC" align="center" prop="price" />
      <el-table-column label="转赠时间" align="center" prop="createDate" />
      <el-table-column v-if="queryParams.type === 1" label="每次消耗积分/SHJC" align="center" prop="feeAmount" />
      <el-table-column v-if="queryParams.type === 1" label="每张消耗积分/SHJC" align="center" prop="cardFeeAmount" />
      <el-table-column v-if="queryParams.type === 1" label="总消耗积分/SHJC" align="center" prop="sumCountJiFen" />
      <el-table-column v-if="queryParams.type === 0" label="转赠积分" align="center" prop="sumCountJiFen" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['log:log:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改转赠记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="转赠人id" prop="transferUserId">
          <el-input v-model="form.transferUserId" placeholder="请输入转赠人id" />
        </el-form-item>
        <el-form-item label="被转赠人id" prop="toTransferUserId">
          <el-input v-model="form.toTransferUserId" placeholder="请输入被转赠人id" />
        </el-form-item>
        <el-form-item label="转赠人姓名" prop="transferName">
          <el-input v-model="form.transferName" placeholder="请输入转赠人姓名" />
        </el-form-item>
        <el-form-item label="被转赠人姓名" prop="toTransferName">
          <el-input v-model="form.toTransferName" placeholder="请输入被转赠人姓名" />
        </el-form-item>
        <el-form-item label="转赠数量/张" prop="transferCardCount">
          <el-input v-model="form.transferCardCount" placeholder="请输入转赠数量/张" />
        </el-form-item>
        <el-form-item label="每张消耗积分/SHJC" prop="cardFeeAmount">
          <el-input v-model="form.cardFeeAmount" placeholder="请输入每张消耗积分/SHJC" />
        </el-form-item>
        <el-form-item label="每次消耗积分/SHJC" prop="feeAmount">
          <el-input v-model="form.feeAmount" placeholder="请输入每次消耗积分/SHJC" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLog,
  getLog,
  delLog,
  addLog,
  updateLog,
  totalTransferList
} from "@/api/system/log/log";

export default {
  name: "Log",
  data() {
    return {
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 转赠记录表格数据
      logList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        transferUserId: null,
        toTransferUserId: null,
        transferName: null,
        toTransferName: null,
        transferCardCount: null,
        cardCountSort: null,
        orderByColumn: null,
        isAsc: 'asc',
        type: 1
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      totalForm: {
        infoTotal: 0,
        balanceTotal: 0,
        cardTotal: 0
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询转赠记录列表 */
    async getList() {
      this.loading = true;
      let params = {
        ...this.queryParams
      };
      if (this.dateRange && this.dateRange.length === 2) {
        params.startTime = this.dateRange[0];
        params.endTime = this.dateRange[1];
      }
      // 处理数量排序
      if (this.queryParams.cardCountSort) {
        params.orderByColumn = 'transfer_card_count';
        params.isAsc = this.queryParams.cardCountSort;
      }
      listLog(params).then((response) => {
        this.logList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      // 统计数据
      const res = await totalTransferList(params);
      if (res.code == 200) {
        this.totalForm = res.data;
      }

    },
    /** 记录类型变化处理 */
    handleTypeChange() {
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        transferId: null,
        transferUserId: null,
        toTransferUserId: null,
        transferName: null,
        toTransferName: null,
        transferCardCount: null,
        createTime: null,
      };
      this.resetForm("form");
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.form = row;
      this.open = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.cardCountSort = null;
      this.queryParams.orderByColumn = null;
      this.queryParams.isAsc = 'asc';
      this.queryParams.type = 0;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.transferId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加转赠记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const transferId = row.transferId || this.ids;
      getLog(transferId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改转赠记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.transferId != null) {
            updateLog(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLog(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const transferIds = row.transferId || this.ids;
      this.$modal
        .confirm('是否确认删除转赠记录编号为"' + transferIds + '"的数据项？')
        .then(function () {
          return delLog(transferIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      if (this.logList.length > 0) {
        let params = {
          type: this.queryParams.type
        };
        if (this.dateRange && this.dateRange.length === 2) {
          params.startTime = this.dateRange[0];
          params.endTime = this.dateRange[1];
        }

        this.download(
          'system/log/export', params,
          `${params.type === 0 ? '积分转赠记录' : '会员卡转赠记录'}数据表${new Date().getTime()}.xlsx`
        );
      } else {
        this.$message.warning("未查询到数据");
      }

    },
  },
};
</script>

<style scoped>
.search-form .el-form-item {
  margin-bottom: 15px;
}
</style>
